<?php

namespace app\admin\controller\teacher;

use app\common\controller\Backend;

class Teacher extends Backend
{
    protected $model = null;

    // 设置当前模型的关联模型
    protected $relationSearch = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Subject.Teacher.Teacher');
        $this->SubjectModel = model('Subject.Subject');
        $this->LikeModel = model('Subject.Teacher.Follow');
        // 查询某一列的值并且指定索引
        // $subject = $this->SubjectModel->select();
        // $likes = $this->LikeModel->select();
        // $subjectList = [];
        // $likesList = [];
        // foreach ($subject as $item)
        // {
        //     $subjectList[$item['id']] =  $item['title'];
        // }
        // foreach ($likes as $item)
        // {
        //     $likesList[$item['id']] =  $item['name'];
        // }
        // $this->view->assign([
        //     'subjectList' => $subjectList,
        //     'likesList' => $likesList
        // ]);
    }

    // 查看
    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $teacherList = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            // 循环遍历查询每个老师的课程数量
            foreach ($teacherList as $k => $v) {
                $teacherList[$k]['subject'] = $this->SubjectModel->where('teacherid', $v['id'])->count();
            }

            // 循环遍历查询每个老师的点赞数量
            foreach ($teacherList as $k => $v) {
                $teacherList[$k]['follows'] = $this->LikeModel->where('teacherid', $v['id'])->count();
            }

            $result = array("total" => $total, "rows" => $teacherList);

            return json($result);
        }
        return $this->view->fetch();
    }

    //添加
    public function add()
    {
        if ($this->request->isPost())
        {
            // 获取表单数据
            $params = $this->request->post('row/a');

            // 添加数据库数据 ，
            $result = $this->model->save($params);

            if ($result === false)
            {
                $this->error($this->model->getError());
                exit;
            }

            $this->success();
        }

        return $this->view->fetch();
    }

    // 编辑
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);

        if (!$row)
        {
            $this->error(__('课程不存在'));
        }

        if ($this->request->isPost())
        {
            // 获取表单数据
            $params = $this->request->post('row/a');
            $params['id'] = $ids;

            $result = $this->model->isUpdate(true)->save($params);

            if ($result === false)
            {
                $this->error($this->model->getError());
                exit;
            }

            //判断是否有上传新图片
            if ($row['avatar'] != $params['avatar'])
            {
                //不相等就说明有换图片了，就删除掉旧图片
                @is_file("." . $row['avatar']) && @unlink("." . $row['avatar']);
            }

            $this->success();
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    // 删除
    public function del($ids = null)
    {
        $row = $this->model->select($ids);

        if (!$row)
        {
            $this->error(__('名师不存在'));
        }

        $result = $this->model->destroy($ids);

        if ($result === false)
        {
            $this->error($this->model->getError());
            exit;
        }

        $this->success();
    }
}
